<?php
/*
	[Destoon B2B System] Copyright (c) 2008-2015 www.destoon.com
	This is NOT a freeware, use is subject to license.txt
*/
defined('IN_DESTOON') or exit('Access Denied');
if($itemid) {
	$item = $db->get_one("SELECT * FROM {$table} WHERE itemid=$itemid");
	($item && $item['status'] > 2) or mobile_msg($L['msg_not_exist']);
	extract($item);
	$CAT = get_cat($catid);
	if(!check_group($_groupid, $MOD['group_show']) || !check_group($_groupid, $CAT['group_show'])) mobile_msg($L['msg_no_right']);
	$description = '';
	$user_status = 3;
	$fee = get_fee($item['fee'], $MOD['fee_view']);
	require 'include/'.($action == 'pay' ? 'pay' : 'content').'.inc.php';
	$content_table = content_table($moduleid, $itemid, $MOD['split'], $table_data);
	$t = $db->get_one("SELECT content FROM {$content_table} WHERE itemid=$itemid");
	$content = video5($t['content']);
	$content = str_replace('<hr class="de-pagebreak" />', '', $content);
	if($user_status == 2) $description = get_description($content, $MOD['pre_view']);
	$editdate = timetodate($addtime, 5);
	$update = '';
	include DT_ROOT.'/include/update.inc.php';
	$head_title = $title.$DT['seo_delimiter'].$MOD['name'].$DT['seo_delimiter'].$head_title;
	$head_name = $CAT['catname'];
	$back_link = 'javascript:Dback(\''.mobileurl($moduleid, $catid).'\', \''.$DT_REF.'\', \'share|comment\');';
	$foot = '';
	//判断权限

require DT_ROOT.'/module/member/member.class.php';
	// 
// inserted by ujh 2016.08.03 -- begin -- 
$check_user_enable = false;
$check_login = false;
$check_article_permision = false;
$check_time = false;
$check_category = false;
$check_area = false;
$check_filtercategory = false;
//$groupfind = array();
$searchday_limit = false;
$check_clicklimit = false;

// get user
$user=array();

if( !empty($_userid) )
{
    $do_member = new member;
    $do_member->userid = $_userid;
    $user = $do_member->get_one();

    if( !empty($user))
    {
        $check_login = true;
    }
}

if( $moduleid != 23 ) // if else zhaobiao 
{
    $check_user_enable = true;
    $check_article_permision = true;
    $check_time = true;
    $check_category = true;
    $check_area = true;
    $check_filtercategory = true;
    $check_clicklimit = true;
}
else
{
    // check category id and filter category and filter address
    // $item['filtercategory'], $item['filteraddress'], $item['catid']
    // $user[zp_starttime], $user[zp_endtime], $user[zp_filteraddress],  $user[zp_filtercategory], $user[zp_category]
    
    if( $user['groupid'] <= 1 )
    {
        $check_user_enable = true;
        $check_time = true;
        $check_category = true;
        $check_area = true;
        $check_filtercategory = true;
        $check_article_permision = true;
        $check_clicklimit = true;
    }
    //else
    //{
    //    $groupfind = $db->get_one("SELECT * FROM {$db->pre}member_group where groupid=".$user['groupid']);
    //}
    
    if( !empty($MG['searchday_limit']) )
        $searchday_limit = $MG['searchday_limit']; 
    
    $dailyclick_limit = 0;
    if( !empty($MG['dailyclick_limit']) )
        $dailyclick_limit = intval ($MG['dailyclick_limit']); 
    
    // get current time 
    $current_time = date("Y-m-d", time());
    
    // if -1 then no limit
    if( $dailyclick_limit < 0 )
    {
        $check_clicklimit = true;
    }
    
    if( $dailyclick_limit > 0 && !empty($do_member) )
    {
        $clicks = $do_member->getDayClicks($current_time);
        if(empty($clicks) || intval($clicks['click_count']) < $dailyclick_limit )
        {
            $check_clicklimit = true;
            $do_member->incClicks();
        }
    }
    
    // check enable
    if( !empty($user['zp_enable']) && ( $user['zp_enable'] == "1" || $user['zp_enable'] == 1 ) )
        $check_user_enable = true;
    
    // check time 
    $start_time = $user['zp_starttime'];
    $end_time = $user['zp_endtime'];
    
    if( !empty($start_time) )
    {
        $check_article_permision = true;
    }
    
    if(  strcmp($start_time, $current_time ) < 0 &&  strcmp($current_time, $end_time ) < 0  )
    {
        $check_time = true;
    }
    
    // check zhaobiao category
    if( empty($user['zp_category']) )
    {
        $check_category = true;
    }
    else
    {
        // check all         
        $pos = strpos($user['zp_category'], "(0)");
        if ($pos !== false) {
            $check_category = true;
        }
        else 
        {
            $pos = strpos($user['zp_category'], "(" . $item['catid'] .  ")");
            if ($pos !== false) {
                $check_category = true;
            }
        }        
    }
    
    // check zhaobiao area
    if( !$check_area && empty($user['zp_filteraddress']) )
    {
        $check_area = true;
    }
    if( !$check_area && empty($item['filteraddress']) )
    {
        $check_area = true;
    }
    
    if( !$check_area )
    {
        // check all         
        $pos = strpos($user['zp_filteraddress'], "(0)");
        if ($pos !== false) {
            $check_area = true;
        }
    }
    
    if( !$check_area )
    {
        // check all         
        $pos = strpos($item['filteraddress'], "(0)");
        if ($pos !== false) {
            $check_area = true;
        }
    }
    
    if( !$check_area )
    {
        $user_areas = array();
        preg_match_all("/\(\d+\)/",  $user['zp_filteraddress'],  $user_areas );
        
        $zb_areas = array();
        preg_match_all("/\(\d+\)/",  $item['filteraddress'],  $zb_areas );
         
        foreach( $user_areas[0] as $ua )
        {
            if( $check_area )
                break;
            
            foreach( $zb_areas[0] as $za )
            {
                if( $check_area )
                    break;
                
                if( strcmp($ua, $za) == 0 )
                {
                    $check_area = true;
                }
            }
        }
    }    
        
    // check zhaobiao filter category
    if( !$check_filtercategory && empty($user['zp_filtercategory']) )
    {
        $check_filtercategory = true;
    }
    if( !$check_filtercategory && empty($item['filtercategory']) )
    {
        $check_filtercategory = true;
    }
    
    if( !$check_filtercategory )
    {
        // check all         
        $pos = strpos($user['zp_filtercategory'], "(0)");
        if ($pos !== false) {
            $check_filtercategory = true;
        }
    }
    
    if( !$check_filtercategory )
    {
        // check all         
        $pos = strpos($item['filtercategory'], "(0)");
        if ($pos !== false) {
            $check_filtercategory = true;
        }
    }
    
    if( !$check_filtercategory )
    {
        $user_areas = array();
        preg_match_all("/\(\d+\)/",  $user['zp_filtercategory'],  $user_areas );
        
        $zb_areas = array();
        preg_match_all("/\(\d+\)/",  $item['filtercategory'],  $zb_areas );
         
        foreach( $user_areas[0] as $ua )
        {
            if( $check_filtercategory )
                break;
            
            foreach( $zb_areas[0] as $za )
            {
                if( $check_filtercategory )
                    break;
                
                if( strcmp($ua, $za) == 0 )
                {
                    $check_filtercategory = true;
                }
            }
        }
    }
}

	
	
	
	
	
	
	
	
	
	
	
} else {
	if($kw) {
		check_group($_groupid, $MOD['group_search']) or mobile_msg($L['msg_no_search']);
	} else if($catid) {
		$CAT or mobile_msg($L['msg_not_cate']);
		if(!check_group($_groupid, $MOD['group_list']) || !check_group($_groupid, $CAT['group_list'])) {
			mobile_msg($L['msg_no_right']);
		}
	} else {
		check_group($_groupid, $MOD['group_index']) or mobile_msg($L['msg_no_right']);
	}
	$head_title = $MOD['name'].$DT['seo_delimiter'].$head_title;
	if($kw) $head_title = $kw.$DT['seo_delimiter'].$head_title;
	$condition = "status=3";
	if($keyword) $condition .= " AND keyword LIKE '%$keyword%'";
	if($catid) $condition .= $CAT ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
	if($areaid) $condition .= $ARE['child'] ? " AND areaid IN (".$ARE['arrchildid'].")" : " AND areaid=$areaid";
	$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE $condition", 'CACHE');
	$items = $r['num'];
	$pages = mobile_pages($items, $page, $pagesize);
	$lists = array();
	if($items) {
		$order = $MOD['order'];
		$time = strpos($MOD['order'], 'add') !== false ? 'addtime' : 'edittime';
		$result = $db->query("SELECT ".$MOD['fields']." FROM {$table} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while($r = $db->fetch_array($result)) {
			if($kw) $r['title'] = str_replace($kw, '<b class="f_red">'.$kw.'</b>', $r['title']);
			$r['linkurl'] = mobileurl($moduleid, 0, $r['itemid']);
			$r['date'] = timetodate($r[$time], $page < 4 ? 2 : 3);
			$lists[] = $r;
		}
		$db->free_result($result);
	}
	$back_link = mobileurl($moduleid);
	if($kw) {
		$head_name = $MOD['name'].$L['search'];
	} else if($catid) {
		$head_name = $CAT['catname'];
		if($CAT['parentid']) $back_link = mobileurl($moduleid, $CAT['parentid']);
	} else {
		$head_name = $MOD['name'];
	}
}
include template($module, 'mobile');
?>